package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.MechanicalLedger;
import net.hn.hnms.biz.project.mapper.MechanicalLedgerMapper;
import net.hn.hnms.biz.project.mapper.ProjectFileMapper;
import net.hn.hnms.biz.project.request.MechanicalLedgerReq;
import net.hn.hnms.biz.project.service.MechanicalLedgerService;
import net.hn.hnms.biz.project.vo.MechanicalLedgerVo;
import net.hn.hnms.biz.project.vo.ProjectInfoVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.tool.Func;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class MechanicalLedgerServiceImpl implements MechanicalLedgerService {


    @Autowired
    private MechanicalLedgerMapper mechanicalLedgerMapper;


    @Autowired
    private ProjectFileMapper projectFileMapper;



    @Override
    public ResponsePage<MechanicalLedgerVo> selectPageList(MechanicalLedgerReq req, PageQuery pageQuery) {
        //先查询项目信息（项目名称和施工单位）
//        List<ProjectInfoVo> projectListByCondition = projectFileMapper.getProjectListByCondition(req.getProjectId(), req.getConstructionUnit());
        List<ProjectInfoVo> projectListByCondition = projectFileMapper.getProjectListByCondition_new(req.getProjectId(),req.getProjectName(), req.getConstructionUnit());
        if (Func.isNotEmpty(projectListByCondition)) {
            //提取projectId列表
            List<String> projectIds = projectListByCondition.stream()
                    .map(ProjectInfoVo::getProjectId)
                    .collect(Collectors.toList());
            req.setProjectIds(projectIds);

            Page<MechanicalLedger> page = mechanicalLedgerMapper.selectPage(pageQuery.build(), buildQuery(req).orderByDesc(MechanicalLedger::getCreateTime));

            List<MechanicalLedgerVo> list = page.getRecords().stream().map(o -> {
                MechanicalLedgerVo vo = new MechanicalLedgerVo();
                BeanUtil.copyProperties(o, vo);

                // 获取项目名称和施工单位信息
                Optional<ProjectInfoVo> projectInfo = projectListByCondition.stream()
                        .filter(p -> p.getProjectId().equals(o.getProjectId()))
                        .findFirst();

                projectInfo.ifPresent(p -> {
                    vo.setProjectName(p.getProjectName());
                    vo.setConstructionUnit(p.getConstructionUnit());
                });

                return vo;
            }).collect(Collectors.toList());
            return new ResponsePage<>(list, page.getTotal());
        }else{
            return new ResponsePage<>(projectListByCondition, 0);
        }

    }

    @Override
    public int insert(MechanicalLedgerReq accidentInfoReq) {
        MechanicalLedger insertObj = new MechanicalLedger();
        BeanUtil.copyProperties(accidentInfoReq, insertObj);
        return mechanicalLedgerMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        mechanicalLedgerMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(MechanicalLedgerReq contractInfoReq) {
        MechanicalLedger updateObj = new MechanicalLedger();
        BeanUtil.copyProperties(contractInfoReq, updateObj);
        int updateCount = mechanicalLedgerMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public MechanicalLedger selectDetailById(String id) {
        MechanicalLedger contractInfo = mechanicalLedgerMapper.selectById(id);
        if (contractInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractInfo;
    }


    private LambdaQueryWrapper<MechanicalLedger> buildQuery(MechanicalLedgerReq req){
        return new LambdaQueryWrapper<MechanicalLedger>()
                .like(StrUtil.isNotBlank(req.getMechanicalName()), MechanicalLedger::getMechanicalName, req.getMechanicalName())
                .in(!CollectionUtils.isEmpty(req.getProjectIds()), MechanicalLedger::getProjectId, req.getProjectIds())
                .eq(StrUtil.isNotBlank(req.getProjectId()), MechanicalLedger::getProjectId, req.getProjectId())
                .eq(StrUtil.isNotBlank(req.getMineCode()), MechanicalLedger::getMineCode, req.getMineCode());
    }
}
